package com.erp.myAdapter

import android.annotation.SuppressLint
import androidx.appcompat.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.erp.R
import com.erp.activity.DanjianivActivity
import com.erp.bean.OemBean
import com.erp.module.oem.OemContract
import com.erp.util.Dialogutil
import com.erp.util.Myutil
import com.erp.util.Sputil
import kotlinx.android.synthetic.main.lv_oem_gongduan_item.view.*

class OemGongduanAdapter (var oemList: MutableList<OemBean.gongxuBean>, var context: Context,val V:OemContract.XxOemView,val P : OemContract.IXxOem): BaseAdapter() {
    var minflater: LayoutInflater = LayoutInflater.from(context)
    var shenhe :Boolean = false
    var width = Sputil.sp("erp").getInt("width", 0)
    @SuppressLint("InflateParams")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

        var v = minflater.inflate(R.layout.lv_oem_gongduan_item,null)
        val bean=oemList[position]
        Myutil.setet(bean.workshop_section_name,v.EtMingcheng)
        var adapter: GvphotoAdapter = GvphotoAdapter(bean.listphoto, context)
        adapter.isadd(true)
        v.GvOemPhoto.adapter = adapter
        v.EtMingcheng.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(p0: Editable?) {
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                bean.workshop_section_name = p0.toString()
            }

        })
        if(bean.listphoto.size < 3 ){
            var params= v.GvOemPhoto.layoutParams
            params.height=width/3;
            v.GvOemPhoto.layoutParams = params;
        }
        if(bean.listphoto.size >= 3 ){
            var params= v.GvOemPhoto.layoutParams
            params.height=width/3 *2;
            v.GvOemPhoto.layoutParams = params;
        }
        if(bean.listphoto.size >= 6 ){
            var params= v.GvOemPhoto.layoutParams
            params.height=width;
            v.GvOemPhoto.layoutParams = params;
        }

        v.GvOemPhoto.onItemClickListener = AdapterView.OnItemClickListener { p0, p1, p2, p3 ->
            if (p2 == bean.listphoto.size) {
                V.kaishiphoto(position)
                Dialogutil.zhiphoto(context, null)
            } else {
                val intent = Intent(context, DanjianivActivity::class.java)
                intent.putExtra("url", bean.listphoto[p2].uri)
                context.startActivity(intent)
            }
        }
        v.GvOemPhoto.onItemLongClickListener = object :AdapterView.OnItemLongClickListener{
            override fun onItemLongClick(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long): Boolean {
                if(shenhe){
                    return true
                }
                val dialog = AlertDialog.Builder(context)
                dialog.setTitle("提示")
                dialog.setMessage("确定要删除这个照片")
                dialog.setPositiveButton("确定") { dialog, which ->
                    bean.deletephoto.add(bean.listphoto[p2])
                    bean.listphoto.remove(bean.listphoto[p2])
                    adapter.notifyDataSetChanged()
                }
                dialog.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
                dialog.show()
                return true
            }
        }
        if(bean.oem_ws_id == ""){
            v.BtShanchu.visibility = View.GONE
            v.BtXiugai.text = "新建工序"
        }
        v.BtXiugai.setOnClickListener {
            bean.workshop_section_name = v.EtMingcheng.text.toString().trim()
            P.postOemPhoto(bean)
        }
        v.BtShanchu.setOnClickListener {
            P.deleteOemPhoto(bean.oem_ws_id)
        }
        return v
    }

    override fun getItem(position: Int): Any = oemList[position]

    override fun getItemId(position: Int): Long = position.toLong()

    override fun getCount(): Int = oemList.size
}